import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_svg/flutter_svg.dart';
import 'package:hktk_app/dao/industry_dao.dart';
import 'package:hktk_app/dao/subject_dao.dart';
import 'package:hktk_app/model/home/column_model.dart';
import 'package:hktk_app/model/subject/chapter_model.dart';
import 'package:hktk_app/widget/tree/chapter_tree.dart';
import 'package:provider/provider.dart';

// 需要所选中科目与所选中栏目
// provider注入科目
class ChapterPracticeList extends StatefulWidget {
  final ColumnModel columnModel;
  final int categoryId;

  ChapterPracticeList(
      {super.key, required this.columnModel, required this.categoryId});

  @override
  State<StatefulWidget> createState() => _ChapterPracticeState();
}

class _ChapterPracticeState extends State<ChapterPracticeList> {
  late ColumnModel _columnModel;
  late List<ChapterModel> chapterList = [];
  late StreamController<bool> _expandedController;
  late int topLevel = 0;

  @override
  void initState() {
    //   initState
    super.initState();
    _columnModel = widget.columnModel;
    _expandedController = StreamController();
    _expandedController.stream.listen((event) {
      if (mounted && event) {
        setState(() {});
      }
    });

    SubjectDao.chapterByColumnId(
            columnId: _columnModel.id!, categoryId: widget.categoryId)
        .then((res) {
      SubjectDao.chapterRecordsIds(
              industryCode: IndustryDao.getExamKey().industryId,
              chapterIds: res.map((e) => e.id!).toList(),
              examId: IndustryDao.getExamKey().examId,
              categoryId: widget.categoryId)
          .then((response) {
        for (var e in res) {
          e.ztNum = response[e.id!]["dc"];
        }
        if (mounted) {
          setState(() {
            chapterList = res;
            if (chapterList.isNotEmpty) {
              topLevel = chapterList[0].level!;
            }
          });
        }
      });
    });
  }

  @override
  void dispose() {
    //   dispose
    super.dispose();
    _expandedController.close();
  }

  @override
  Widget build(BuildContext context) {
    // 
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: Text(_columnModel.name!),
      ),
      body: SafeArea(
        child: Container(
          margin: EdgeInsets.all(16),
          height: 1.sh -
              ScreenUtil().statusBarHeight -
              kToolbarHeight -
              ScreenUtil().bottomBarHeight -
              36,
          decoration: BoxDecoration(
            color: Colors.white,
            borderRadius: BorderRadius.all(Radius.circular(10)),
          ),
          child: chapterList.isNotEmpty
              ? SingleChildScrollView(
                  child: MultiProvider(
                      providers: [
                        Provider<Map<String, dynamic>>(create: (context) {
                          return {
                            "columnId": _columnModel.id,
                            "topLevel": topLevel
                          };
                        }),
                      ],
                      child: Column(
                        children: [
                          // Center(
                          // child: SvgPicture.asset("images/home/empty.svg",
                          //   width: 1.sw - 36,))

                          for (var i = 0; i < chapterList.length; i++) ...[
                            Padding(
                                padding: EdgeInsets.all(8),
                                child: ChapterTree(
                                    key: ValueKey(chapterList[i].id),
                                    free: chapterList[i].free ?? true,
                                    controller: _expandedController,
                                    chapter: chapterList[i])),
                            Container(
                              width: 1.sw - 32,
                              height: 5,
                              decoration: BoxDecoration(
                                  color: Theme.of(context).colorScheme.surface),
                            )
                          ]
                        ],
                      )),
                )
              : Center(
                  child: SvgPicture.asset(
                    "images/home/empty.svg",
                    width: 1.sw - 36,
                  ),
                ),
        ),
      ),
    );
  }
}
